<?php
/**
 * Created by PhpStorm.
 * User: Earl
 * Date: 2016/11/4
 * Time: 16:21
 */
namespace Push\Controller;
use Api\Controller\BaseApiController;
use User\Model\UserModel;
use Message\Model\MessageModel;

class ApiController extends BaseApiController
{
    private $user_db;
    private $redbag_db;
    private $mid;

    function _initialize()
    {
        parent::_initialize();
        $this->user_db = new UserModel();
    }
    /**
     * 抢红包推送消息
     * @param $uid 当前登录的用户id
     * @param $fromid 发送方的用户id
     * @param $toid 目标方id
     * @param $type 1单人，2群聊
     */
    function sendRedbagNotice($fromid,$toid,$type,$redbagid){
        $msg = new MessageModel();
        $uid=I('uid');
        if($type==1){
            $toResult=M('user')->where(array('uid'=>$toid))->find();
            $toname=$toResult['nickname'];
            $tourl=$toResult['headsmall'];
            $typechat=100;
        }else{
            $typechat=300;
            $toname="";
            $tourl="";
        }
        $fromResult=M('user')->where(array('uid'=>$fromid))->find();
        $fromname=$fromResult['nickname'];
        $fromurl=$fromResult['headsmall'];
        $rCount=M('redbag')->where(array('id'=>$redbagid))->getField('quantity');
        $hCount=M('redbag_detail')->where(array('redbag_id'=>$redbagid))->count();
        $cCount=$rCount-$hCount;
        if($cCount==1){
            $content=$fromname.'领取了一个红包，红包已领完';
        }else{
            $content=$fromname.'领取了一个红包';
        }


        $voicetime=0;
        $return = $msg->sendRedbagMessage($fromid,$fromname,$fromid,$fromurl,$toid,$toname,$tourl,$content,$voicetime,$typechat,11,$redbagid);
        if($return['state']['code']==0){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 发送红包结果通知
     * @param $redbag_id
     * @param $uid 当前登录用户的id？
     */
    function sendRedbagResult($redbag_id){
        $map['id']=array('eq',$redbag_id);
        $sessionid=M('redbag')->where($map)->getField('sessionid');
        $redbagResult=M('redbag')->where($map)->find();

        $redbag_content=$redbagResult['keywords'];
        $sessionResult=M('session')->where(array('id'=>$sessionid,'status'=>1))->find();

        if ($sessionResult['notice_type']==0) {
            return false;
        }

        $result=M('redbag_detail')->where(array('redbag_id'=>$redbag_id))->select();
        switch ($sessionResult['notice_type']){
            case 1://接龙
                $tempResult=M('redbag_detail')->where(array('redbag_id'=>$redbag_id))->order('score desc')->select();
                $pushRessult=M('push')->select();
                $temp=array();
                $pushArr=array();
                foreach ($tempResult as $k=>$v){
                    $temp[$k]['uid']=$v['uid'];
                    $temp[$k]['nickname']=M('user')->where(array('uid'=>$v['uid']))->getField('nickname');
                    $temp[$k]['score']=$v['score'];
                    $temp[$k]['time']=$v['create_time'];
                    if(!empty($pushRessult)){
                        foreach ($pushRessult as $pk=>$pv){
                            if($pv['keywords']==$v['score']){
                                $temp[$k]['keywords']="1";
                            }else{
                                $temp[$k]['keywords']="0";
                            }
                        }
                    }else{
                        $temp[$k]['keywords']="0";
                    }
                    $pushArr[$k]=M('user')->where(array('uid'=>$v['uid']))->getField('nickname').'本次领取的红包为：'.$v['score'].'元';
                }
//                $content='红包通知【接龙】：'.implode(',',$pushArr);
                $content=json_encode(array('title'=>$redbag_content,'list'=>$temp,'type'=>'1'));
                break;
            case 2://扫雷
                $temp=array();
                foreach ($result as $k=>$v){
                    $n=$v['score'] * 100;
                    $s=substr($n,-1,1);
                    $temp[$k]['uid']=$v['uid'];
                    $temp[$k]['nickname']=M('user')->where(array('uid'=>$v['uid']))->getField('nickname');
                    $temp[$k]['score']=$s;
                    $temp[$k]['t_score']=$v['score'];
                    $temp[$k]['time']=$v['create_time'];
                }


                $temp=my_sort($temp,'score',SORT_DESC,SORT_STRING);
                $content=json_encode(array('title'=>$redbag_content,'list'=>$temp,'type'=>'2'));
                break;
            case 3://牛牛
                $temp=array();
                $pushTemp=array();
                foreach ($result as $k => $v){
                    $n=ceil(substr($v['score'],-4,4)*100);
                    $s=0;
                    while ( $n > 0 ) { $s += $n % 10; $n /= 10; }
                    $s=substr($s,-1,1);
                    $temp[$k]['uid']=$v['uid'];
                    $temp[$k]['nickname']=M('user')->where(array('uid'=>$v['uid']))->getField('nickname');
                    $temp[$k]['score']=$s;
                    $temp[$k]['t_score']=$v['score'];
                    $temp[$k]['time']=$v['create_time'];
                    $pushTemp[$k]=M('user')->where(array('uid'=>$v['uid']))->getField('nickname').'本次领取的为牛'.$s;
                }

                /**
                 * 把发包人排在第一位
                 */
                $tArr=array();
                foreach ($temp as $tk=>$tv){
                    if($tv['uid']==$redbagResult['uid']){
                        $tArr['uid']=$tv['uid'];
                        $tArr['nickname']=$tv['nickname'];
                        $tArr['score']=$tv['score'];
                        $tArr['t_score']=$tv['t_score'];
                        $tArr['time']=$tv['time'];
                        unset($temp[$tk]);
                        array_unshift($temp,$tArr);
                    }
                }
//                $content='红包通知【牛牛】：'.implode(',',$pushTemp);
                $content=json_encode(array('title'=>$redbag_content,'list'=>$temp,'type'=>'3'));
                break;
        }
        $msg = new MessageModel();
        $fromid=-1;

        $typechat=600;
        $fromname='系统通知';
        $fromurl='http://120.27.51.67/Public/logo.png';

        $toid=$sessionResult['uid'];
        $toname=$sessionResult['name'];
        $tourl='http://120.27.51.67/Public/logo.png';

        $voicetime=0;
        $typefile=10;

        $return = $msg->sendRedbagMessage($fromid,$fromname,$fromid,$fromurl,$toid,$toname,$tourl,$content,$voicetime,$typechat,$typefile);

        if($return['status']['code']==0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 给某人发消息
     * @param $uid
     * @param $toid
     * @param $content titile,list,type
     * @return bool
     */
    function sendMsgToUser($toid,$content,$uid){

        $uid = $uid > 0?$uid:$uid=I('uid');
        $msg = new MessageModel();
        $fromid=-1;
        $typechat=600;
        $fromname='系统通知';
        $fromurl='http://120.27.51.67/Public/logo.png';

        $toResult=M('user')->where(array('uid'=>$toid))->find();
        $toname=$toResult['nickname'];
        $tourl=$toResult['headsmall'];
        $voicetime=0;
        $content=json_encode($content);
        $return = $msg->sendRedbagMessage($uid,$fromname,$fromid,$fromurl,$toid,$toname,$tourl,$content,$voicetime,$typechat,10);

        if($return['code']==0){
            return true;
        }else{
            return false;
        }
    }


    function sendMsgToSession($joinid,$toid){
        $msg = new MessageModel();
        $uid=I('uid');

        $typechat=300;
        $toname="";
        $tourl="";

        $fromid="";
        $fromname="群聊消息";
        $fromurl="";

        $joinName=M('user')->where(array('uid'=>$joinid))->getField('nickname');
        $content=$joinName."加入了群聊";

        $voicetime=0;
        $return = $msg->sendRedbagMessage($fromid,$fromname,$fromid,$fromurl,$toid,$toname,$tourl,$content,$voicetime,$typechat,12);
        if($return['state']['code']==0){
            return true;
        }else{
            return false;
        }
    }


}